<?php
/*
 * Created on Feb 5, 2009
 * @author: ANH
 * @ fileName: /modules/Form/Place.php
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
 class place_Form_Place extends Zend_Form
 {
 	 protected $cName = 'place_Form_Place';
     protected $_noDecorator = array(
        'ViewHelper', 
        'Errors'
    );
    
    /**
     * init() is the initialization routine called when Zend_Form objects are 
     * created. In most cases, it make alot of sense to put definitions in this 
     * method, as you can see below.  This is not required, but suggested.  
     * There might exist other application scenarios where one might want to 
     * configure their form objects in a different way, those are best 
     * described in the manual:
     *
     * @see    http://framework.zend.com/manual/en/zend.form.html
     * @return void
     */ 
    public function cluBuild($params)
    {
        $filterTrim = new Zend_Filter_StringTrim();
        $filterFloat = new Zend_Validate_Float();
        $validatorNotEmpty = new Zend_Validate_NotEmpty();
        $myValidator = new Zend_Validate_Regex('/^[A-Z0-9_\s\.\-]*$/i'); //alpha, num, space, -, _
        $myValidator->setMessage(t(60), Zend_Validate_Regex::NOT_MATCH);
        // set the method for the display form to POST
        $this->setMethod('POST');
        
        $this->setAction($params['action_url']);
		
		if($params['action'] === 'add-article' )
        {
        	//namePlace: 
			$nameplace = new Zend_Form_Element_Text('name', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   /*'required' => true*/));
	        $nameplace->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim)
	                  ->addValidator($validatorNotEmpty)
	                  /*->addValidator($myValidator) TODO*/;
	        $this->addElement($nameplace);
	        
	        $weight = new Zend_Form_Element_Text('article_weight', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   /*'required' => true*/));
	        $weight->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim)
	                  ->addValidator($validatorNotEmpty);
	        $this->addElement($weight);
	        
	        /*
	        //add keywords
	        $keywords = new Zend_Form_Element_Text('article_keywords', 
	                         array('disableLoadDefaultDecorators' => true,
	                               'class' => 'inputText'));
	        
	        $keywords->addDecorators($this->_noDecorator)
	              ->addFilter($filterTrim)
	              ->addValidator($myValidator);
	        $this->addElement($keywords);
        	*/
	        //language
	 		$language = new Zend_Form_Element_Select('article_language',  
	                         array('disableLoadDefaultDecorators' => true,
	                                /*'required' => true*/));
	        $language->addMultiOption('vi',t(40))
	                   ->addMultiOption('en',t(41))
	                   ->addMultiOption('cn',t(42))
	                   ->addMultiOption('jp',t(43))
	               	   ->addDecorators($this->_noDecorator);
	        $this->addElement($language);
	        /*
	        //category
	        $category = new Zend_Form_Element_Select('article_module',  
	                         array('disableLoadDefaultDecorators' => true,
	                                'required' => true));
	        $category->addMultiOption('0','--'.t(97).'--')
	               ->addMultiOption('blog',t(98))
	               ->addMultiOption('forum',t(99))
	               ->addMultiOption('tour',t(801))
	               ->addMultiOption('place',t(701))
	               ->addMultiOption('event',t(201))
	               ->addMultiOption('news',t(301))
	               ->addMultiOption('other',t(100))
	               ->addDecorators($this->_noDecorator);
			
	        $this->addElement($category);
	        */
	         //story body
	        $fck = new Clu_Form_Element_FckEditor('article_body', array(
	        		'fckOptions' => array (
	        						'Height' => 600,
	        						'Width'  => 630,
	        						'ToolbarSet' => 'Full'
	        					)
	       			 )
	        );
	        $fck->setLabel(t(742));
	        $this->addElement($fck);
        }
        else
        {
	   		$sess = new Zend_Session_Namespace(CLU_NS);
//	   		$perm = strpos ($sess->userPermissions, 'set status place');
	   		
	 		// Category
	 		$rtype = new Zend_Form_Element_Select('rtype',  
	 											  array('disableLoadDefaultDecorators' => true,
										          ));
			$mCategory = new category_Model_Category();
			$mCategory->setTables('place');
	        $rtype->addMultiOption('0','--'.t(113).'--');
	        foreach ($mCategory->getRootType('Root_Place') as $c) {
	            $rtype->addMultiOption($c['id'], p(trim($c['name'])));
	        	$rtype->addDecorators($this->_noDecorator);
	        }
	        $this->addElement($rtype);
	        /*
	        //rtype
	        $type = new Zend_Form_Element_Select('type',  array('disableLoadDefaultDecorators' => true,
	                                                                 'required' => false));
			$type->addMultiOption('0','--'.t(112).'--');                                                                 
			$type->setRegisterInArrayValidator(false)
				  ->addDecorators($this->_noDecorator);
	        $this->addElement($type);
	        */
	        //namePlace: 
			$nameplace = new Zend_Form_Element_Text('name', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => true));
	        $nameplace->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim)
	                  ->addValidator($validatorNotEmpty)
	                  /*->addValidator($myValidator) TODO*/;
	        $this->addElement($nameplace);
	        
	        //language
	 		$language = new Zend_Form_Element_Select('language',  
	                         array('disableLoadDefaultDecorators' => true,
	                                'required' => true));
	        $language->addMultiOption('vi',t(40))
	                   ->addMultiOption('en',t(41))
	                   ->addMultiOption('cn',t(42))
	                   ->addMultiOption('jp',t(43))
	               	->addDecorators($this->_noDecorator);
	        if($params['action'] == 'new')
	        {
	       		$language->setValue($params['language']);
	        }
	        $this->addElement($language);
	        
	        //other_name
	        $other_name = new Zend_Form_Element_Text('other_name', array('disableLoadDefaultDecorators' => true,
		                                                                     'class' => 'inputText',
		                                                                   ));
		    $other_name->addDecorators($this->_noDecorator)
		    		   ->addFilter($filterTrim)
		               /*->addValidator($myValidator)*/;
		    $this->addElement($other_name);
		    
	        //City
	        $city_id = new Zend_Form_Element_Select('city_id',array('disableLoadDefaultDecorators' => true,
	                                                             'required' => true));
	        $city_id->addMultiOption(null,'--'.t(717).'--');
	        $table = new place_Model_City();
	        $results = $table->fetchAll();
	//        $rets = $results->toArray();
	        
	        $topCities = Zend_Registry::get('configuration')->topCities->toArray();
	        $city_id->addMultiOptions($topCities)
			        ->setAttrib('disable', array('1a'));
	
	        foreach ($results as $c) {
	//        	if (!array_key_exists($c->city_id, $topCities)) //only add non-top cities
		            $city_id->addMultiOption($c->city_id, $c->city_name);
	        }
	    	$city_id->addDecorators($this->_noDecorator);
	        $this->addElement($city_id);
	        
	        //District
	        $district_id = new Zend_Form_Element_Select('district_id',array('disableLoadDefaultDecorators' => true,
	                                                                 'required' => true));
	        $district_id->addMultiOption('0','--'.t(718).'--')
	                    ->setRegisterInArrayValidator(false)	
	                    ->addDecorators($this->_noDecorator);
	 
	        $this->addElement($district_id);
	        
	        //Village
	        $village_id = new Zend_Form_Element_Select('village_id',array('disableLoadDefaultDecorators' => true,
	                                                                 'required' => true));
	        $village_id->addMultiOption('0','--'.t(720).'--')
	                   ->setRegisterInArrayValidator(false)	
	                   ->addDecorators($this->_noDecorator);
	 
	        $this->addElement($village_id);
	        
	        
	        // Street_Name
	        $str_name = new Zend_Form_Element_Text('address5', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => false));
	        $str_name->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim)
	                  /*->addValidator($myValidator)*/;
	        $this->addElement($str_name);
	        
	        // Street_Number
	        $str_number = new Zend_Form_Element_Text('address6', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => false));
	        $str_number->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim)
	                  /*->addValidator($myValidator)*/;
	        $this->addElement($str_number);
	        
	        // Thon_Khu Pho
	        $address7 = new Zend_Form_Element_Text('address7', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => false));
	        $address7->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim)
	                  /*->addValidator($myValidator)*/;
	        $this->addElement($address7);
	        
	        // Xom_Ap_Doi
	        $address8 = new Zend_Form_Element_Text('address8', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => false));
	        $address8->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim)
	                  /*->addValidator($myValidator)*/;
	        $this->addElement($address8);
	        
	        // The rest
	        $address9 = new Zend_Form_Element_Text('address9', array('disableLoadDefaultDecorators' => true,
	                                                                   'class' => 'inputText',
	                                                                   'required' => false));
	        $address9->addDecorators($this->_noDecorator)
	                  ->addFilter($filterTrim)
	                  /*->addValidator($myValidator)*/;
	        $this->addElement($address9);
	        
	        //longitude
	        $longitude = new Zend_Form_Element_Hidden('longitude', array('disableLoadDefaultDecorators' => true,
	                                                                   /*'class' => 'inputText',*/
	                                                                   ));
	        $longitude->addDecorators($this->_noDecorator)
	                  ->addValidator($filterFloat);
	        $this->addElement($longitude);
	        
	        //latitude
	        $latitude = new Zend_Form_Element_Hidden('latitude', array('disableLoadDefaultDecorators' => true,
	                                                                 /*'class' => 'inputText',*/
	                                                                 ));
	        $latitude->addDecorators($this->_noDecorator)
	                 ->addValidator($filterFloat);
	        $this->addElement($latitude);
	
	        if ($params['action'] === 'new' && (strpos ($params['userPermissions'], 'set status place') === false))
			{
	            //add check captcha
	            $elementCaptcha = 
	            				   new Zend_Form_Element_Captcha('captcha_newplace',
	                               array('captcha' => 'Image','captchaOptions' =>
	                               array(
	                                    'captcha' => 'Image',
	                                    'wordLen' => 4,
	                                    'timeout' => 300,
	                                    'imgDir'  =>APP_PATH.'/public/captcha',
	                                    'imgUrl' => '/public/captcha/',
	                                    'width' => 175,
	                                    'height' => 60,
	                                    'font' => APP_PATH.'/public/captcha/font/tahoma.ttf',
	                                    'DotNoiseLevel' => 10,  
	                                    'LineNoiseLevel' => 1,
	                                    'expiration' => 3,
	                                    'gcFreq' => 1))
	            );
	            $elementCaptcha->setLabel(t(39).':')
	                           ->setAttrib('id','captcha_newplaceImage')
	                       	   ->setRequired(true);
	            $this->addElement($elementCaptcha);
	            
			}            
	        
	        if (strpos ($params['userPermissions'], 'set status place'))
	        {
	            $status = new Zend_Form_Element_Select('status',array('disableLoadDefaultDecorators' => true,
	                                                                     'required' => true));
	        	$status	->addMultiOption('featured',t(33))
	            	   ->addMultiOption('published',t(34))
	                   ->addMultiOption('queued',t(35))
	                   ->addMultiOption('unpublished',t(36))
	                   ->addMultiOption('deleted',t(37))
	                    ->setRegisterInArrayValidator(false)	
	                    ->addDecorators($this->_noDecorator);
	            $this->addElement($status);
	        }
        }
        // add the submit button
		if ($params['action'] === 'new' || $params['action'] === 'add-article')
    		$submit = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true, 
                                                           'label' => t(58) ));
      	else
      		$submit = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true, 
                                                               'label' => t(59) ));
        
        $submit->addDecorators($this->_noDecorator);
        $this->addElement($submit);
    }
    
    public function populateCategory($data, $categoryId)
    {
    	//Populate rtype options 
    	if ($categoryId != 0)
		{
			$mCategory = new category_Model_Category();
			$mCategory->setTables('place');
		    $categories = $mCategory->getTree($categoryId, 1, 1);
		    function cmp($a, $b)
			{
			    return strcmp($a['name'], $b['name']);
			}
			usort($categories, "cmp");
		    
			foreach ($categories as $c) {
				$c = (array)$c;
				if($c['id'] != 0)
	          		$this->rtype->addMultiOption($c['id'], $c['displayName']);
     		}
		}
        //Populate district options 
    	if (isset($data['city_id']) && $data['city_id'] != 0)
	    {
	       	$mDistrict = new place_Model_District();
	    	$districts = $mDistrict->getDistricts($data['city_id']);
			foreach ($districts as $d) {
          	    $this->district_id->addMultiOption($d['district_id'], $d['district_name']);
     		}
        }
        //populate village options
    	if (isset($data['district_id']) && $data['district_id'] != 0)
	    {
			$mVillage = new place_Model_Village();
		    $villages = $mVillage->getVillages($data['district_id']);
			foreach ($villages as $v) {
          	    $this->village_id->addMultiOption($v['village_id'], $v['village_name']);
     		}
        }
        $data['category'] = $categoryId;
        $this->populate($data);
    }
 }
?>
